package com.zuche.seraph.agent.config;


import com.alibaba.fastjson.JSON;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

public class ProxyInfoHelper {





    public static ProxyInfo getProxyInfo(){
        return new ProxyInfo();
    }


    /**
     * 获取proxy信息
     * @param host - proxy的ip或域名
     * @param application - 应用
     * @return proxyInfo
     */
    public static ProxyInfo getAuth(String host, String application) {
        // 获取token地址
        String proxyHost = "http://"+host;
        String proxyUrl = proxyHost + "/seraph/getProxyInfo?&application=" + application;
        try {
            URL realUrl = new URL(proxyUrl);
            // 打开和URL之间的连接
            HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                System.err.println(key + "--->" + map.get(key));
            }
            // 定义 BufferedReader输入流来读取URL的响应
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String result = "";
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            /**
             * 返回结果示例
             */
            System.err.println("result:" + result);
            ProxyInfo proxyInfo = JSON.parseObject(result,ProxyInfo.class);
            return proxyInfo;
        } catch (Exception e) {
            System.err.printf("获取proxyInfo失败！");
            e.printStackTrace(System.err);
        }
        return null;
    }


}
